<template>
  <el-dialog
    title="选择商品"
    :visible.sync="dialogVisible"
    width="845px"
    :before-close="handleClose"
    class="material"
    style="user-select: none;"
  >
    <div v-if="show">
      <div class="clearfix">
        <div
          class="img_box"
          style="background:#fff;margin:10px 22px;padding-bottom:5px;height:290px;"
        >
          <div
            v-for="(imgItem, imgIndex) in getImgList"
            :key="imgIndex"
            class="img_item_border"
            :style="{ borderColor: imgItem.check ? '#3587fc' : '' }"
            @click="checkHandle(img_index)"
          >
            <img :src="imgItem.src" />
            <div style="height:63px;position:relative;">
              <div style=" width: 66px; height: 42px; overflow: hidden;">
                {{ imgItem.goods_name }}
              </div>
              <div style="position:absolute;bottom:0px;">
                {{ imgItem.goods_price }}
              </div>
            </div>
          </div>
          <div
            v-if="getImgList.length <= 0"
            style="text-align:center;margin:150px 0;"
          >
            您暂未添加商品
          </div>
        </div>
      </div>
      <div style="text-align: right; height:88px;">
        <div style="text-align:center;padding-top:10px;">
          <el-pagination
            small
            background
            layout="pager,jumper"
            :page-size="15"
            :total="listLenght"
            @current-change="changeCurrentPage"
          >
          </el-pagination>
        </div>
        <span slot="footer" class="dialog-footer" style="margin:0 12px;">
          <el-button size="small" @click="handleClose">取 消</el-button>
          <el-button size="small" type="primary" :disabled="an" @click="next"
            >下一步</el-button
          >
        </span>
      </div>
    </div>
    <div v-else>
      <div class="clearfix">
        <div
          class="img_box"
          style="background:#fff;padding-bottom:5px;height:328px;width:100%;"
        >
          <el-table
            :data="attrArr"
            :span-method="objectSpanMethod"
            max-height="320"
          >
            <el-table-column label="规格" align="center" width="80">
              <template slot-scope="scope">
                <span style="font-size:12px;">
                  {{ scope.row.sku_type[0] }}
                </span>
              </template>
            </el-table-column>
            <el-table-column width="80">
              <template slot-scope="scope">
                <span style="font-size:12px;">
                  {{ scope.row.sku_type[1] }}
                </span>
              </template>
            </el-table-column>
            <el-table-column width="80">
              <template slot-scope="scope">
                <span style="font-size:12px;">
                  {{ scope.row.sku_type[2] }}
                </span>
              </template>
            </el-table-column>
            <el-table-column align="center" :label="comName + '价格（元）'">
              <template slot-scope="scope">
                <span style="font-size:12px;">
                  原价格（元）：{{ scope.row.sku_price }}
                  <el-input-number
                    v-model="attrArr[scope.$index].set_price"
                    style="width:120px"
                    size="mini"
                    :precision="2"
                    :controls="false"
                  ></el-input-number>
                </span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="库存">
              <template slot-scope="scope">
                <span style="font-size:12px;">
                  原库存{{ scope.row.sku_stock }}件
                </span>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div style="text-align: right;height:50px;">
        <span slot="footer" class="dialog-footer" style="margin:0 12px;">
          <el-button size="small" type="primary" @click="addImg"
            >保存</el-button
          >
        </span>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { GOODSLIST } from "@/service/public";

export default {
  props: {
    ware: {
      type: Boolean,
      default: false
    },
    comName: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      listLenght: 0,
      dialogVisible: this.ware,
      show: true,
      getImgList: [],
      rootlist: [],
      an: true,
      attrArr: []
    };
  },
  watch: {
    ware(val) {
      this.dialogVisible = val;
    },
    dialogVisible(v) {
      if (v === false) {
        this.show = true;
      }
    }
  },
  created() {
    this.changeCurrentPage();
    this.cont = "";
  },
  methods: {
    async changeCurrentPage(val) {
      let params = {
        flie_type: 1,
        page: val || 1,
        pageshow: 15
      };
      await GOODSLIST(params).then(({ code, data }) => {
        if (code === 200) {
          if (data.length > 0) {
            this.getImgList = [];
            data.list.forEach(item => {
              if (!item) {
                return;
              }
              this.getImgList.push({
                goods_name: item.goods_name,
                goods_price: item.goods_price,
                goods_origin_price: item.goods_original_price,
                check: false,
                src: `${this.$store.state.qiniu}/${item.goods_cover}`,
                goods_id: item.goods_id,
                goods_content: item.goods_content,
                attr_arr: JSON.parse(item.attrArr),
                sku_type: JSON.parse(item.sku_type)
              });
            });

            this.listLenght = data.length;
          }
        }
      });
      if (this.idx === 1) {
        this.cont = "";
        this.getImgList.forEach(ele => {
          ele.check = false;
        });
      }
    },
    handleClose() {
      this.dialogVisible = false;
      this.$emit("close");
    },
    checkHandle(i) {
      if (this.cont !== "") {
        this.getImgList[this.cont].check = !this.getImgList[this.cont].check;
      }
      this.getImgList[i].check = !this.getImgList[i].check;

      this.cont = i;
      this.getImgList.forEach(ele => {
        if (ele.check === true) {
          this.an = false;
        }
      });
    },
    next() {
      this.show = false;
      this.rootlist = this.getImgList.filter(item => {
        return item.check === true;
      });
      let set = {
        set_price: ""
      };
      this.rootlist[0].attrArr.forEach(ele => {
        Object.assign(ele, set);
      });
      this.attrArr = this.rootlist[0].attr_arr;
    },
    addImg() {
      let s = 0;
      let i = 0;
      let shu = 1;

      this.rootlist[0].attr_arr = this.attr_arr;
      this.rootlist[0].attr_arr.forEach(ele => {
        shu = parseInt(ele.sku_stock) + i;
        i = parseInt(ele.sku_stock);
        if (ele.set_price === "") {
          return (s = 1);
        }
      });
      if (s === 1) {
        this.$message.error("请正确添加数值");
      } else {
        this.$emit("postImgs", this.rootlist[0], shu);
        this.$emit("close");
        this.dialogVisible = false;
      }
    },
    objectSpanMethod({ row, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        if (row.sku_type[1] && !row.sku_type[2]) {
          if (rowIndex % row.sku_type.length === 0) {
            return {
              rowspan: row.sku_type.length,
              colspan: 1
            };
          } else {
            return {
              rowspan: 0,
              colspan: 0
            };
          }
        } else if (row.sku_type[1] && row.sku_type[2]) {
          if (rowIndex % (row.sku_type.length * row.sku_type.length) === 0) {
            return {
              rowspan: row.sku_type.length * row.sku_type.length,
              colspan: 1
            };
          } else {
            return {
              rowspan: 0,
              colspan: 0
            };
          }
        }
      }
      if (columnIndex === 1) {
        if (row.sku_type[2]) {
          if (rowIndex % row.sku_type.length === 0) {
            return {
              rowspan: row.sku_type.length,
              colspan: 1
            };
          } else {
            return {
              rowspan: 0,
              colspan: 0
            };
          }
        }
      }
    }
  }
};
</script>

<style scoped lang="scss">
.el-pagination {
  white-space: initial;
}
.currentImg {
  border: 2px solid #00ffff !important;
}
.form-select {
  width: 100px;
  transform-origin: left;
  margin-left: 20px;
}
.form-radio {
  font-size: 12px;
  transform-origin: left;
}
.form-inline-form {
  width: 200px;
  display: flex;
  flex-direction: row;
  align-items: top;
  justify-content: space-around;
  display: inline-block;
  margin-left: 160px;
  .form-inline-form-from {
    width: 80px;
  }
  .form-inline-form-to {
    width: 80px;
  }
}
.form-search {
  width: 200px;
  margin-left: 20px;
}
.form-input1 {
  position: relative;
  &::before {
    content: "价格";
    font-size: 12px;
    left: -49px;
    position: absolute;
  }
}
.form-input2 {
  position: relative;
  margin-left: 20px;
  &::before {
    content: "--";
    font-size: 12px;
    left: -25px;
    display: block;
    position: absolute;
    color: grey;
  }
}
.img_box {
  .img_item_border {
    float: left;
    z-index: 999999999;
    border: 1px solid rgba(grey, 0.4);
    height: 90px;
    width: 150px;
    margin: 5px 5px 0px;
    cursor: pointer;
    z-index: 99;
    border-radius: 5px;
    display: flex;
    align-items: center;
    img {
      width: 63px;
      height: 63px;
      border-radius: 5px;
      margin-left: 5px;
    }
    div {
      margin-left: 3px;
      color: rgba(grey, 0.8);
      text-align: left;
    }
  }
}

.clearfix:before,
.clearfix:after {
  content: " ";
  display: table;
}
.clearfix:after {
  clear: both;
}
</style>
